import HttpClient from '../commons/http-client';
import {updateCustomerList, resetCustomerList} from '../reducers/customer-list';

export function loadCustomerList(payload) {
  return async dispatch => {
    const info = {
      ...payload,
      page_size: 10,
      expand: 'avatar,hold_card,plan,logo',
    };
    const url = `/admin/dentist?${HttpClient.encodeParams(info)}`;
    const customerListRep = await HttpClient.get(url);
    dispatch(updateCustomerList(customerListRep.data.data));
  };
}

export function disableCustomer(payload) {
  return async (dispatch, getState) => {
    const {customerList} = getState();
    const customerRep = await HttpClient.put(
      `/admin/dentist/${payload.id}`,
      payload,
    );
    customerList.results = customerList.results.map(val =>
      val.id === customerRep.data.data.id ? customerRep.data.data : val,
    );
    dispatch(updateCustomerList(customerList));
  };
}

export function reviewCustomer(payload) {
  return async dispatch => {
    const url = `/admin/dentist/${payload.id}`;
    await HttpClient.put(url, payload);
    dispatch(resetCustomerList());
  };
}
